package cn.cyh.user.service.impl;

import cn.cyh.Dto.UserDto;
import cn.cyh.basic.exception.BusinessException;
import cn.cyh.basic.service.impl.BaseServiceImpl;
import cn.cyh.basic.uitls.MD5Utils;
import cn.cyh.basic.uitls.StrUtils;
import cn.cyh.org.mapper.ShopMapper;
import cn.cyh.user.contants.UserContants;
import cn.cyh.user.domain.LoginInfo;
import cn.cyh.user.domain.User;
import cn.cyh.user.mapper.LoginInfoMapper;
import cn.cyh.user.mapper.UserMapper;
import cn.cyh.user.service.IUserService;
import netscape.security.UserTarget;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


// 支持事务 (如果有事务就加入事务，没有就不加入)
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    //  自动注入
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public void register(UserDto userDto) {
        // 校验是否填写信息
        if (StringUtils.isEmpty(userDto.getPhone())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getVerifyCode())
                || StringUtils.isEmpty(userDto.getConfirmPwd())){
            throw new BusinessException("请填写信息");
        }
        // 校验两次密码
        if (!userDto.getPassword().equals(userDto.getConfirmPwd())) {
            throw new BusinessException("两次密码不一致");
        }
        // 校验是否被注册
        User userTmp = userMapper.loadByPhone(userDto.getPhone());
        if (userTmp != null) {
            throw new BusinessException("该账号已经被注册，请直接登录");
        }
        // 校验验证码
        Object codeTmp = redisTemplate.opsForValue().get(UserContants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        if (codeTmp == null) {
            throw new BusinessException("验证码已经过期 请重新获取 ");
        }
        // 不区分大小写
        String code = codeTmp.toString().split(":")[0];
        if (!userDto.getVerifyCode().equalsIgnoreCase(code)) {
            throw new BusinessException("验证码错误");
        }

         // 保存数据
        User user = userDto2User(userDto);
        // 先保存loginInfo
        LoginInfo loginInfo = User2LoginInfo(user);
        // 要返回自增id
        loginInfoMapper.save(loginInfo);

        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);
    }

    private LoginInfo User2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        // 属性同名拷贝
        BeanUtils.copyProperties(user,loginInfo);
        loginInfo.setType(1);
        return loginInfo;
    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        // 盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 加密加盐
        String byMd5 = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(byMd5);
        return user;
    }
}
